﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace FrameEngine
{
    /// <summary>
    /// 命令基类
    /// </summary>
    public abstract class BaseCommand : ICommand
    {
        private readonly IApplication _application;

        private string _caption = "";
        private bool _checked;
        private ICommandUI _commandUI = new NullCommandUi();
        private bool _enabled = true;
        private ImageSource _imageSource;
        private string _tooltip = "";
        private bool _visible = true;
        private string _name = "";


        /// <summary>
        /// 命令基类
        /// </summary>
        /// <param name="pApplication"></param>
        public BaseCommand(IApplication pApplication, string pName)
        {
            _application = pApplication;
            this._name = pName;
            _application.RegisterCommand(this);
        }


        /// <summary>
        /// 命令基类
        /// </summary>
        /// <param name="pApplication"></param>
        public BaseCommand(IApplication pApplication)
            : this(pApplication, "")
        {
        }


        #region ICommand Members

        /// <summary>
        /// 得到主操作对象
        /// </summary>
        public IApplication Application
        {
            get { return _application; }
        }


        /// <summary>
        /// 名称
        /// </summary>
        public string CommandName
        {
            get { return this.GetType().ToString() + this._name; }
        }



        /// <summary>
        /// 得到或设置显示的标题
        /// </summary>
        public string Caption
        {
            get { return _caption; }
            set
            {
                _caption = value;
                _commandUI.SetCaption(value);
            }
        }


        /// <summary>
        ///  得到或设置图标
        /// </summary>
        public ImageSource ImageSource
        {
            get { return _imageSource; }
            set
            {
                _imageSource = value;
                _commandUI.SetImageSource(value);
            }
        }


        /// <summary>
        /// 得到或设置工具提示信息
        /// </summary>
        public string ToolTip
        {
            get { return _tooltip; }
            set
            {
                _tooltip = value;
                _commandUI.SetToolTip(value);
            }
        }


        /// <summary>
        /// 得到或设置当前工具的选中状态
        /// </summary>
        public bool IsChecked
        {
            get { return _checked; }
            set
            {
                //为了把菜单和工具条上的按钮状态同步更新
                List<ICommand> myCommandList = _application.GetCommandsByKey(this.CommandName);
                foreach (ICommand myCommand in myCommandList)
                {
                    var myBaseCommand = myCommand as BaseCommand;
                    myBaseCommand._checked = value;
                    myBaseCommand._commandUI.SetCheckedState(value);
                }
            }
        }

        /// <summary>
        /// 得到或设置是否可用
        /// </summary>
        public bool IsEnabled
        {
            get { return _enabled; }
            set
            {
                _enabled = value;
                _commandUI.SetEnabledState(value);
            }
        }


        /// <summary>
        /// 得到或设置是否可见
        /// </summary>
        public bool IsVisible
        {
            get { return _visible; }
            set
            {
                _visible = value;
                _commandUI.SetVisibleState(value);
            }
        }


        /// <summary>
        /// 点击命令执行的函数
        /// </summary>
        public virtual void OnClick()
        {
            //命令不需要checked
            IsChecked = false;
        }


        /// <summary>
        /// 设置UI
        /// </summary>
        /// <param name="pCommandUi"></param>
        public void SetCommandUI(ICommandUI pCommandUi)
        {
            _commandUI = pCommandUi;
            _commandUI.SetCaption(_caption);
            _commandUI.SetCheckedState(_checked);
            _commandUI.SetEnabledState(_enabled);
            _commandUI.SetImageSource(_imageSource);
            _commandUI.SetToolTip(_tooltip);
            _commandUI.SetVisibleState(_visible);
        }


        /// <summary>
        /// 更新工具是否可见状态
        /// </summary>
        public virtual void UpdateVisibleState()
        {
        }


        /// <summary>
        /// 更新工具是否可用状态
        /// </summary>
        public virtual void UpdateEnableState()
        {
        }

        #endregion
    }
}
